<?php
/**
 * Description of TransDetail 交易详情共用处理
 *
 * @author Mike Gao
 */
class TransDetailAction extends CommonAction{
    
    /**
     *update stock,
     * write log
     * update Finance TODO
     * @param <type> $field default is name
     */
    public  function update($field='name') {
        //B('FilterString');
        $name=$this->getActionName();
        $p_name = str_replace('_detail', '', $name);    //Parent Action Name (OverView Action)
        $model = D ( $name );
        $id = intval($_POST['id']);
        $old = $model->where('id = '.$id)->find();
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        // 更新数据
        //对应总价，总成本要同时更新 TODO
        $list=$model->save ();
        if (false !== $list) {
            $new = $model->where('id = '.$id)->find();

            //如果修改了数量，则同时更新库存
            if(($diff = ( $new['number'] -  $old['number']) ) != 0 && $p_name != 'Broken'){
                $product_id = getModelName($id, $name, 'product_id');
                if ($p_name == 'Switch') {
                    A('Trans')->updateStock($product_id, $new['from_warehouse_id'], 'out', $diff);
                    A('Trans')->updateStock($product_id, $new['to_warehouse_id'], 'in', $diff);
                } else {
                    $warehouse_id = getModelName($new[strtolower($p_name) . '_id'], $p_name, 'warehouse_id');
                    if ($p_name == 'Sale' || $p_name == 'Back_supplier')
                        $type = 'out';
                    if ($p_name == 'Sale_back' || $p_name == 'Enter')
                        $type = 'in';
                    A('Trans')->updateStock($product_id, $warehouse_id, $type, $diff);
                }
            }

            //如果修改了单价，则同时更新财务记录    TODO

            $mod_field = getModelName ($id, $name,$field);
            //如果未取到字段值 ，则返回对应的ID编号
            $msg = '修改 '.$name.': '.$mod_field.' (编号：'.$id.')';
            if('Action' != $name)
                A('Action')->writeLog($msg,'修改',$name,$id,$old,$new);
            //成功提示
            $jumpUrl = $_REQUEST['jumpUrl'];
            if(empty($jumpUrl))
                $jumpUrl = Cookie::get ( '_currentUrl_' );
            $this->assign ( 'jumpUrl', $jumpUrl );
            $this->success ('编辑成功!');
        } else {
            //错误提示
            $this->error ('编辑失败!');
        }
    }


    /**
     * 插入同时，记录相应的日志
     * @param <string> $filed  记录的字段名称，默认为name
     */
    function insert($field='name') {
        //B('FilterString');
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
        //update stock
        $parent_name = str_replace('_detail', '', $name);    //Parent Action Name (OverView Action)
        
        
        if( $parent_name != 'Broken'){
                $product_id = $_POST['product_id'];
                $diff = $_POST['number'];

                if ($p_name == 'Switch') {
                    A('Trans')->updateStock($product_id, $_POST['from_warehouse_id'], 'out', $diff);
                    A('Trans')->updateStock($product_id, $_POST['to_warehouse_id'], 'in', $diff);
                } else {
                    $warehouse_id = getModelName( $_POST[ strtolower($parent_name).'_id'],$parent_name,'warehouse_id');
                    if ($parent_name == 'Sale' || $parent_name == 'Back_supplier')
                        $type = 'out';
                    if ($parent_name == 'Sale_back' || $parent_name == 'Enter')
                        $type = 'in';
                    A('Trans')->updateStock($product_id, $warehouse_id, $type, $diff);
                }
            }

            //如果修改了单价，则同时更新财务记录    TODO

            /******************日志记录*********************/
             $mod_field = getModelName ($list, $name,$field);
            //如果未取到字段值 ，则返回对应的ID编号
            $msg = '新增 '.$name.': '.$mod_field.' (编号：'.$list.')';
            if('Action' != $name)
                A('Action')->writeLog($msg,'新增',$name,$list);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success ('新增成功!');
        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }

}
?>
